package com.ruoyi.platform.system.role.controller;

import com.ruoyi.base.annotaion.PermissionCheck;
import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.page.PageData;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.utils.arrays.ArrayUtils;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.platform.system.menu.enums.MenuEnum;
import com.ruoyi.platform.system.menu.pojo.MenuTreeDTO;
import com.ruoyi.platform.system.role.pojo.RoleDO;
import com.ruoyi.platform.system.role.service.RoleDTO;
import com.ruoyi.platform.system.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息表
 *
 * @author lcf
 * @version 0.0.1
 * @since 2020-11-16 01:47:41
 */
@Api(tags = {"角色管理"})
@RestController
@Scope("request")
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     */
    @ApiOperation("新增角色")
    @PostMapping
    @PermissionCheck(MenuEnum.ROLE_MANAGEMENT_ADD)
    public HttpResult<String> addRole(@ApiParam(value = "角色名称", required = true) String roleName) {
        InputCheckUtils.checkStr(roleName, "请输入角色名称");

        roleService.addRole(roleName);
        return success();
    }

    /**
     * 修改角色
     */
    @ApiOperation("修改角色")
    @PatchMapping
    @PermissionCheck(MenuEnum.ROLE_MANAGEMENT_EDIT)
    public HttpResult<String> editRole(@ApiParam(value = "角色id", required = true) long roleId,
                                       @ApiParam(value = "角色名称", required = true) String roleName) {
        InputCheckUtils.checkStr(roleName, "请输入角色名称");
        InputCheckUtils.checkLong(roleId, "请选择角色");

        roleService.editRole(roleId, roleName);
        return success();
    }

    /**
     * 删除角色
     */
    @ApiOperation("删除角色")
    @DeleteMapping
    @PermissionCheck(MenuEnum.ROLE_MANAGEMENT_DEL)
    public HttpResult<String> delRole(@ApiParam(value = "角色id", required = true) long roleId) {
        InputCheckUtils.checkLong(roleId, "请选择角色");

        roleService.delRole(roleId);
        return success();
    }

    /**
     * 批量删除角色
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("/list")
    @PermissionCheck(MenuEnum.ROLE_MANAGEMENT_DEL)
    public HttpResult<String> delRoleList(@ApiParam(value = "角色id列表", required = true) Long[] roleIdList) {
        InputCheckUtils.checkArray(roleIdList, "请选择角色");

        roleService.delRoleList(ArrayUtils.arrays2List(roleIdList));
        return success();
    }

    /**
     * 获取角色列表
     */
    @ApiOperation("获取角色列表")
    @GetMapping("/list")
    public HttpResult<PageData<RoleDO, RoleDTO>> getRoleList(@ApiParam("角色名称") String roleName,
                                                             @ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                                                             @ApiParam("每页条目数") @RequestParam(defaultValue = "20") int pageSize) {
        return data(roleService.getRoleList(roleName, pageNum, pageSize));
    }

    /**
     * 设置角色菜单权限
     */
    @ApiOperation("设置角色菜单权限")
    @PatchMapping("/menu")
    @PermissionCheck(MenuEnum.ROLE_MANAGEMENT_PERMISSION)
    public HttpResult<String> editRoleMenu(@ApiParam(value = "角色id", required = true) long roleId,
                                           @ApiParam(value = "菜单id列表") String[] menuCodeList) {
        InputCheckUtils.checkLong(roleId, "请选择角色");

        roleService.editRoleMenu(roleId, ArrayUtils.arrays2List(menuCodeList));
        return success();
    }

    /**
     * 获取角色菜单列表
     */
    @ApiOperation("获取角色菜单列表")
    @GetMapping("/list/menu")
    public HttpResult<List<MenuTreeDTO>> getRoleMenuTree(@ApiParam(value = "角色id", required = true) long roleId) {
        InputCheckUtils.checkLong(roleId, "请选择角色");

        return data(roleService.getRoleMenuTree(roleId));
    }
}